package jk.wrapper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import jk.wrapper.builder.xml.PropertyMapParser;
import jk.wrapper.common.util.Resources;
import jk.wrapper.common.util.StringUtil;
import jk.wrapper.config.PropertyMapConfig;
import jk.wrapper.config.WrapperConfiguration;
import jk.wrapper.handle.TagHandle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Wrapper {
	private static final Log log = LogFactory.getLog(Wrapper.class);
	
	private WrapperConfiguration wrapperConfig;
	
	public Wrapper(String confFile) {
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReaderByFile(confFile, "utf-8");
		} catch (IOException e) {
			log.error("Load config file error", e);
		}
		wrapperConfig = new PropertyMapParser().parse(reader);
	}
	
	public ResultMap parse() {
		System.out.println(wrapperConfig.getResultMapConfig().size());
		ResultMap retMap = new ResultMap();
		Map<String, PropertyMapConfig> propMap = wrapperConfig.getPropertyMapConfig();
		Iterator<Entry<String, PropertyMapConfig>> it = propMap.entrySet().iterator();
		while(it.hasNext()) {
			Entry<String, PropertyMapConfig> entry = it.next();
			String id = entry.getKey();
			PropertyMapConfig propConfig = entry.getValue();
			
			ResultBean retBean = new ResultBean(id, propConfig.getField(), propConfig.getDefValue());
			List<ResultBean> retBeanList = new ArrayList<ResultBean>();
			retBeanList.add(retBean);
			Resource res = new Resource();
			res.setUrl("http://localhost");
			retMap.setResource(res);
			retMap.setHtmlSource(readFile("wrapperTest.html"));
			retMap.setCurPropId(id);
			retMap.putResultMap(id, retBeanList);
			for(TagHandle tagHandle : propConfig.getTagHandleList()) {
				tagHandle.handle(retMap);
			}
			
		}
		return retMap;
	}
	
	public String readFile(String fileName) {
		BufferedReader in = null;
		String line;
		StringBuffer ret = new StringBuffer();
		try {
			in = new BufferedReader(new FileReader(new File(fileName)));
			line = in.readLine();
			ret.append(line);
			while(line != null || !StringUtil.isStringNull(line)){
				ret.append(line);
				line = in.readLine();
			}
			in.close();
		} catch (IOException e) {
			log.error("IOException", e);
		} finally {
			try {
				if(in != null)
					in.close();
			} catch (IOException e) {
				log.error("IOException", e);
			}
		}
		return ret.toString();
	}
	
	public static void main(String[] args) {
		Wrapper wrapper = new Wrapper("config/dianping.xml");
		ResultMap retMap = wrapper.parse();
		System.out.println(retMap.getResultBeanValue("url").resultVaild());
		System.out.println(retMap.getResultBeanValue("url").getField());
		System.out.println(retMap.getResultBeanValue("url").getValue());
		System.out.println();
		System.out.println(retMap.getResultBeanValue("tag").resultVaild());
		System.out.println(retMap.getResultBeanValue("tag").getField());
		System.out.println(retMap.getResultBeanValue("tag").getValue());
		System.out.println();
		System.out.println(retMap.getResultBeanValue("price").resultVaild());
		System.out.println(retMap.getResultBeanValue("price").getField());
		System.out.println(retMap.getResultBeanValue("price").getValue());
		System.out.println();
		System.out.println(retMap.getResultBeanValue("score").resultVaild());
		System.out.println(retMap.getResultBeanValue("score").getField());
		System.out.println(retMap.getResultBeanValue("score").getValue());
		System.out.println();
		List<ResultBean> retList = retMap.getResultBean("comments");
		for(ResultBean retBean : retList) {
			System.out.println();
			System.out.println(retBean.resultVaild());
			System.out.println(retBean.getField());
			System.out.println(retBean.getValue());
		}
		
		System.out.println();
	}
}
